package com.bob.clan.wxconnector.wxService;


import com.bob.clan.wxconnector.component.RestApiComponent;
import com.bob.clan.wxconnector.config.WxServerConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class RidService {
    @Autowired
    private WxServerConfig wxServerConfig;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RestApiComponent restApiComponent;

    public String getRidDetail(String sysCode, String rid) throws JsonProcessingException {
        String token = tokenService.getToken(sysCode);
        String url = wxServerConfig.getUrlByToken(wxServerConfig.getRidUrl(), token);
        Map<String,Object> params = new HashMap<>();
        params.put("rid",rid);
        String detail = restApiComponent.post(url,params);
        log.info(detail);
        return detail;
    }

    public String getRid(String sysCode, String rid) throws JsonProcessingException {
        String token = tokenService.getToken(sysCode);
        String url = wxServerConfig.getUrlByToken(wxServerConfig.getRidUrl(), token);
        RestTemplate restTemplate = new RestTemplate();
        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("rid", rid);

        // 将请求体转换为JSON字符串
        HttpEntity<String> requestEntity = new HttpEntity<>(new MappingJackson2HttpMessageConverter().getObjectMapper().writeValueAsString(requestBody), headers);

        // 调用外部API
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

        // 返回响应体
        return response.getBody();
    }
}
